package top.zhangjianyong.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.entity.po.BlogCommentLike;
import top.zhangjianyong.blog.mapper.BlogCommentLikeMapper;
import top.zhangjianyong.blog.service.BlogCommentLikeService;

/**
 * 博客评论点赞服务实现类
 *
 * @author zhangjianyong
 * @since 2024/12/19
 */
@Service
public class BlogCommentLikeServiceImpl extends ServiceImpl<BlogCommentLikeMapper, BlogCommentLike>
    implements BlogCommentLikeService {

  @Override
  public boolean hasLiked(Long commentId, String visitorUuid) {
    return lambdaQuery()
        .eq(BlogCommentLike::getCommentId, commentId)
        .eq(BlogCommentLike::getVisitorUuid, visitorUuid)
        .exists();
  }

  @Override
  public boolean addLike(Long commentId, String visitorUuid, Long userId) {
    BlogCommentLike like =
        new BlogCommentLike().setCommentId(commentId).setVisitorUuid(visitorUuid).setUserId(userId);
    return save(like);
  }

  @Override
  public boolean removeLike(Long commentId, String visitorUuid) {
    return lambdaUpdate()
        .eq(BlogCommentLike::getCommentId, commentId)
        .eq(BlogCommentLike::getVisitorUuid, visitorUuid)
        .remove();
  }

  @Override
  public long getLikeCount(Long commentId) {
    return lambdaQuery().eq(BlogCommentLike::getCommentId, commentId).count();
  }
}
